package com.czy.owner.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.DetectionResult;
import com.czy.owner.ui.userinfo.UserInfoActivity;
import com.czy.owner.utils.glide.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by King on 2017-05-16
 */

public class DetectionResultAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<DetectionResult.ModelsListBean> detectionResults = new ArrayList<>();

    public DetectionResultAdapter(Context mContext, List<DetectionResult.ModelsListBean> detectionResults) {
        this.mContext = mContext;
        this.detectionResults = detectionResults;
    }

    @Override
    public int getGroupCount() {
        return detectionResults.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return detectionResults.get(groupPosition).getSonModelsList().size();
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }


    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return detectionResults.get(groupPosition).getSonModelsList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return detectionResults.get(groupPosition);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder = null;
        if (convertView == null) {
            groupViewHolder = new GroupViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_detection_result_group, parent, false);
            groupViewHolder.tvGroupName = (TextView) convertView.findViewById(R.id.tv_item_group_name);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.tvGroupName.setText(detectionResults.get(groupPosition).getModelsName());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder = null;
        if (convertView == null) {
            childViewHolder = new ChildViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_detection_result_list, parent, false);
            childViewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_item_name);
            childViewHolder.ivLogo = (ImageView)convertView.findViewById(R.id.tv_item_logo);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
         childViewHolder.tvName.setText(detectionResults.get(groupPosition).getSonModelsList().get(childPosition).getModelsName());
        childViewHolder.ivLogo.setTag(detectionResults.get(groupPosition).getSonModelsList().get(childPosition));
        if(TextUtils.isEmpty(detectionResults.get(groupPosition).getSonModelsList().get(childPosition).getModelsLogo())){
            childViewHolder.ivLogo.setImageResource(R.mipmap.default_icon);
        }else{
            GlideUtils.loadImage(mContext, detectionResults.get(groupPosition).getSonModelsList().get(childPosition).getModelsLogo(), childViewHolder.ivLogo, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                }

                @Override
                public void onLoadingError(String source, Exception e) {
                }
            }, R.mipmap.default_icon, R.mipmap.default_icon);
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupViewHolder {
        TextView tvGroupName;
    }

    class ChildViewHolder {
        ImageView ivLogo;
        TextView tvName;

    }
}
